//
//  OceanBaseVC.m
//  OceanStaffApp
//
//  Created by Zheng on 2019/7/31.
//  Copyright © 2019 Zh. All rights reserved.
//

#import "OceanBaseVC.h"

@interface OceanBaseVC ()

@property (nonatomic, copy) void(^leftClickBlock)(NSInteger index);
@property (nonatomic, copy) void(^rightClickBlock)(NSInteger index);

@end

@implementation OceanBaseVC

- (instancetype)init {
    self = [super init];
    if (self) {
        _canInteractivePopGesture = YES;
        _haveNavBarLine = NO;
        _hiddenNavBar = NO;
    }
    return self;
}

#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = CX_GetColorLine();

    if (_haveNavBarLine) {
        [self.navigationController.navigationBar setShadowImage:nil];
    } else {
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    }

    if (self.navigationController.viewControllers.count > 1 && _canInteractivePopGesture) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
        backButton.frame = CGRectMake(0,0,40,40);
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
        [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.navigationController.viewControllers.count > 1 && _canInteractivePopGesture) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    } else {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (_hiddenNavBar) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (_hiddenNavBar) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

#pragma mark - 生成通用模式按钮
- (UIButton *)getCommonButtonWithTag:(NSInteger)tag withSel:(SEL)sel {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0,0,40,40);
    btn.tag = tag;
    [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

#pragma mark - 设置导航栏左侧按钮
- (void)configLeftItemWithTitleArr:(NSArray *)arr withClickBlock:(void(^)(NSInteger index))block {
    _leftClickBlock = block;
    NSMutableArray *itemsArr = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [self getCommonButtonWithTag:1000+idx withSel:@selector(leftItemAction:)];
        btn.titleLabel.font = CX_GetNormalFont(15);
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:CX_GetColorTint() forState:UIControlStateNormal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [itemsArr addObject:item];
    }];
    self.navigationItem.leftBarButtonItems = itemsArr;
}

- (void)configLeftItemWithImageArr:(NSArray *)arr withClickBlock:(void(^)(NSInteger index))block {
    _leftClickBlock = block;
    NSMutableArray *itemsArr = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [self getCommonButtonWithTag:1000+idx withSel:@selector(leftItemAction:)];
        [btn setImage:[UIImage imageNamed:obj] forState:UIControlStateNormal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [itemsArr addObject:item];
    }];
    self.navigationItem.leftBarButtonItems = itemsArr;
}

- (void)leftItemAction:(UIButton *)sender {
    Available_Block(_leftClickBlock)(sender.tag-1000);
}


#pragma mark - 设置导航栏右侧按钮
- (void)configRightItemWithTitleArr:(NSArray *)arr withClickBlock:(void(^)(NSInteger index))block {
    _rightClickBlock = block;
    NSMutableArray *itemsArr = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [self getCommonButtonWithTag:2000+idx withSel:@selector(rightItemAction:)];
        btn.titleLabel.font = CX_GetNormalFont(15);
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:CX_GetColorTint() forState:UIControlStateNormal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [itemsArr addObject:item];
    }];
    self.navigationItem.rightBarButtonItems = itemsArr;
}

- (void)configRightItemWithImageArr:(NSArray *)arr withClickBlock:(void(^)(NSInteger index))block {
    _rightClickBlock = block;
    NSMutableArray *itemsArr = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [self getCommonButtonWithTag:2000+idx withSel:@selector(rightItemAction:)];
        [btn setImage:[UIImage imageNamed:obj] forState:UIControlStateNormal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [itemsArr addObject:item];
    }];
    self.navigationItem.rightBarButtonItems = itemsArr;
}

- (void)rightItemAction:(UIButton *)sender {
    Available_Block(_rightClickBlock)(sender.tag-2000);
}

#pragma mark -
- (void)backAction {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.operationQueue cancelAllOperations];

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self cyl_pushViewController:viewController animated:animated];
}



@end
